/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sct_beans;

import entities.TestItem;
import executor.*;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.LocalBean;
import org.omg.CORBA.ORB;
import org.omg.CosNaming.NamingContextExt;
import org.omg.CosNaming.NamingContextExtHelper;
import testing.TestingException;

/**
 *
 * @author ryuju
 */
@Singleton
@LocalBean
public class SCExecuteBean {

    private SC_Executor executor = null;

    @PostConstruct
    private void init() {
        try {
            Properties p = new Properties();
            p.put("org.omg.CORBA.ORBInitialPort", "1050");
            p.put("org.omg.CORBA.ORBInitialHost", "localhost");

            // create and initialize the ORB
            ORB orb = ORB.init((String[]) null, p);

            // get the root naming context
            org.omg.CORBA.Object objRef =
                    orb.resolve_initial_references("NameService");
            // Use NamingContextExt instead of NamingContext. This is 
            // part of the Interoperable naming Service.  
            NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);

            // Find (resolve) reference to the server object and narrow
            // it down to Hello type.

            String name = "SC_Executor";
            executor = (SC_Executor) SC_ExecutorHelper.narrow(ncRef.resolve_str(name));
        } catch (Exception e) {
            Logger.getLogger(SCExecuteBean.class.getName()).log(Level.SEVERE, null, e);;
        }
    }

    private ExecutionItem createExecutionItem(TestItem testItem) {
        ExecutionItem execItem = new ExecutionItem();
        execItem.inputs = new String[testItem.getTestInputs().size()];
        execItem.inputs = testItem.getTestInputs().toArray(execItem.inputs);
        execItem.maxmumExecMem = 0;
        execItem.maxmumExecTime = 0;
        execItem.srcCode = testItem.getSourceCode();
        execItem.srcLang = testItem.getSourceLang();
        return execItem;
    }

    public ExecutionResult execute(TestItem testItem) throws TestingException {

        ExecutionResult execResult = getEmptyExecutionResult();

        if (null == testItem) {
            throw new TestingException("TestItem is null");
        }

        try {
            ExecutionItem execItem = createExecutionItem(testItem);
            execResult = executor.execute(execItem);
        } 
        catch(ExecutingException ee)
        {
            throw new TestingException(ee.msg);
        }
        catch (Exception e) {
            throw new TestingException("Execution Fail");
        }

        return execResult;
    }

    private ExecutionResult getEmptyExecutionResult() {
        ExecutionResult execResult = new ExecutionResult();

        execResult.errMsg = "Empty Result";
        execResult.errOccured = false;
        execResult.execCompleted = false;
        execResult.execTime = 0;
        execResult.exitCode = 0;
        execResult.outputs = new String[0];

        return execResult;
    }
}
